package neusoft.edu.cn.a215lmeordering.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mob.MobSDK;

import java.util.Timer;
import java.util.TimerTask;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import neusoft.edu.cn.a215lmeordering.R;

public class Login_phone_Activity extends AppCompatActivity {
    int flag_textView=0;
    private int recLen = 60;


    Button login;//登录
    TextView sendCode;//发送验证码
    TextView password_login;//转换登录方式
    EditText phone;
    EditText verification_code;

    EventHandler eventHandler = new EventHandler(){
        public void afterEvent(int event, int result, Object data){
            Message msg = new Message();
            msg.arg1 = event;
            msg.arg2 = result;
            msg.obj = data;
            handler.sendMessage(msg);
        }
    };

    Handler handler = new Handler(){
        public void handleMessage(Message msg){
            int event = msg.arg1;
            int result = msg.arg2;
            Object data = msg.obj;

            if (result == SMSSDK.RESULT_COMPLETE) {//回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功
                    Toast.makeText(getApplicationContext(), "提交验证码成功",Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent(Login_phone_Activity.this,MainActivity.class);
                    intent1.putExtra("user",phone.getText().toString());
                    startActivity(intent1);
                    finish();
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    Toast.makeText(getApplicationContext(), "正在获取验证码", Toast.LENGTH_SHORT).show();
                } else {
                    ((Throwable) data).printStackTrace();
                    Toast.makeText(getApplicationContext(), "可能失败了...", Toast.LENGTH_SHORT).show();
                }
            }
            else {
                Toast.makeText(getApplicationContext(), "验证失败", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_login_phone);

        init();

        //短信验证
        MobSDK.init(this);
        // 注册一个事件回调，用于处理SMSSDK接口请求的结果
        SMSSDK.registerEventHandler(eventHandler);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 提交验证码，其中的code表示验证码，如“1357”
                SMSSDK.submitVerificationCode("86", phone.getText().toString(), verification_code.getText().toString());
            }
        });

        sendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (flag_textView == 0) {
                    flag_textView=1;

//                    //使按钮不可点击
//                    sendCode.setClickable(false);
                    String phoneNums = phone.getText().toString();
                    SMSSDK.getVerificationCode("86", phoneNums);

                    final Timer timer = new Timer();
                    final Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            switch (msg.what) {
                                case 1:
                                    sendCode.setText("已发送（" + recLen + ")");
                                    if (recLen < 0) {
                                        timer.cancel();
                                        recLen = 60;
                                        flag_textView=0;

////                                      使按钮不可点击
//                                        sendCode.setClickable(true);
                                        sendCode.setText("发送验证码");
                                    }
                            }
                        }
                    };

                    TimerTask task = new TimerTask() {
                        @Override
                        public void run() {
                            recLen--;
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                        }
                    };

                    timer.schedule(task, 1000, 1000);
                    Toast.makeText(getApplicationContext(), "验证码发送成功", Toast.LENGTH_SHORT).show();


                }else{

                    Toast.makeText(getApplicationContext(), "请耐心等候", Toast.LENGTH_LONG).show();
                }
            }

        });

        password_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void init() {
        sendCode = findViewById(R.id.sendCode);//发送验证码
        login = findViewById(R.id.button1);//登录
        password_login = findViewById(R.id.password_login);
        phone = findViewById(R.id.phone);
        verification_code = findViewById(R.id.verification_code);
    }

    @Override
    protected void onDestroy() {
        // 使用完EventHandler需注销，否则可能出现内存泄漏
        SMSSDK.unregisterAllEventHandler();
        super.onDestroy();
    }
}
